import request from "../sevices/request";

class Login {
  static sendCode = (tel: string) => {
    if (tel.length < 11) {
      return { data: { message: "请输入正确手机号" }, code: 111 }
    } else {
      let code = (Math.random() + "").split(".")[1].substring(0, 5)
      request.post("/code", { tel, code })
      return { data: { code, message: "获取验证码成功" }, code: 200 }
    }

  }
  static checkTel = async (tel: string) => {
    let res = await request.get("/user", { params: { tel } })
    if (res.data.length === 0) {
      return { data: { message: "手机号未注册" }, code: 200 }
    } else {
      return { data: { message: "手机号已注册" }, code: 111 }
    }
  }
  static doRegister = (obj: any) => {
    let data = { ...obj, token: obj.userid }
    request.post("/user", data)
    return { data: { data: data, message: "注册成功" }, code: 200 }
  }
  static login = async (obj: any) => {
    let { tel, password } = obj
    let res = await request.get("/user", { params: { tel, password } })
    if (res.data.length === 0) {
      return { data: { message: "密码错误" }, code: 111 }
    } else {
      return { data: { data: res.data, message: "登录成功" }, code: 200 }
    }

  }
  static checkCode = async (tel: string, code: string) => {
    let res = await request.get("/code", { params: { code, tel } })
    if (res.data.length === 0) {
      return { data: { message: "验证码错误" }, code: 111 }
    } else {
      return { data: { message: "验证码正确" }, code: 200 }
    }
  }

}


export default Login